<!doctype html>
<html xml:lang="zh-CN" lang="zh-CN">

<head>
        <link rel="canonical" href="https://v2rayjichang.github.io/news/article-98091.htm" />
    <!--====== Required meta tags ======-->
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Ribbon（一）远程调用RestTemplate</title>
        <meta name="description" content="目录 一、RestTemplate介绍 二、需求说明 1、业务结构及说明 三、模块搭建 1、新建一个maven项目 2、添加ribbon依赖 3、编辑yml 4、主程序 5、编辑RibbonContr" />
        <link rel="icon" href="/assets/website/img/surfboardnode/favicon.ico" type="image/x-icon"/>

    <meta name="author" content="V2ray免费机场 节点订阅中文官网">
    <meta property="og:type" content="article" />
    <meta property="og:url" content="https://v2rayjichang.github.io/news/article-98091.htm" />
    <meta property="og:site_name" content="V2ray免费机场 节点订阅中文官网" />
    <meta property="og:title" content="Ribbon（一）远程调用RestTemplate" />
    <meta property="og:image" content="https://v2rayjichang.github.io/uploads/20240709-1/310cab61201208c0b3f68c60d4d9cf84.webp" />
        <meta property="og:release_date" content="2025-04-17T09:01:37" />
    <meta property="og:updated_time" content="2025-04-17T09:01:37" />
        <meta property="og:description" content="目录 一、RestTemplate介绍 二、需求说明 1、业务结构及说明 三、模块搭建 1、新建一个maven项目 2、添加ribbon依赖 3、编辑yml 4、主程序 5、编辑RibbonContr" />
        
    <meta name="applicable-device" content="pc,mobile" />
    <meta name="renderer" content="webkit" />
    <meta name="force-rendering" content="webkit" />
    <meta http-equiv="Cache-Control" content="no-transform" />
    <meta name="robots" content="max-image-preview:large" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-title" content="Ribbon（一）远程调用RestTemplate">
    <meta name="format-detection" content="telephone=no">

    <link rel="dns-prefetch" href="https:/www.googletagmanager.com">
    <link rel="dns-prefetch" href="https://www.googleadservices.com">
    <link rel="dns-prefetch" href="https://www.google-analytics.com">
    <link rel="dns-prefetch" href="https://pagead2.googlesyndication.com">
    <link rel="dns-prefetch" href="https://cm.g.doubleclick.net">
    
    <!--====== Bootstrap css ======-->
    <link rel="stylesheet" href="/assets/website/css/surfboardnode/bootstrap.min.css">
    <!--====== Slick css ======-->
    <link rel="stylesheet" href="/assets/website/css/surfboardnode/slick.css">
    <!--====== Magnific Popup css ======-->
    <link rel="stylesheet" href="/assets/website/css/surfboardnode/magnific-popup.css">
    <!--====== Line Icons css ======-->
    <link rel="stylesheet" href="/assets/website/css/surfboardnode/LineIcons.css">
    <!--====== Default css ======-->
    <link rel="stylesheet" href="/assets/website/css/surfboardnode/default.css">
    <!--====== Style css ======-->
    <link rel="stylesheet" href="/assets/website/css/surfboardnode/style.css">
    <!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-LMZ4WN7LCM"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-LMZ4WN7LCM');
</script>
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3332997411212854"
     crossorigin="anonymous"></script>
</head>

<body data-page="detail">
    <!--====== HEADER ONE PART START ======-->
    <header class="header-area">
        <div class="navbar-area navbar-one navbar-transparent">
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <nav class="navbar navbar-expand-lg">
                    <a class="navbar-brand" href="/">
                                        <span>
                        V2ray免费机场                    </span>
                                        </a>
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarOne" aria-controls="navbarOne" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="toggler-icon"></span>
                        <span class="toggler-icon"></span>
                        <span class="toggler-icon"></span>
                    </button>
                    <div class="collapse navbar-collapse sub-menu-bar" id="navbarOne">
                        <ul class="navbar-nav m-auto">
                                                        <li class="nav-item">
                                <a href="/">首页</a>
                            </li>
                                                        <li class="nav-item">
                                <a href="/free-nodes/">免费节点</a>
                            </li>
                                                        <li class="nav-item">
                                <a href="/paid-subscribe/">推荐机场</a>
                            </li>
                                                        <li class="nav-item">
                                <a href="/news/">新闻资讯</a>
                            </li>
                                                        <li class="nav-item">
                                <a href="/client.htm">客户端</a>
                            </li>
                                                    </ul>
                    </div>
                </nav> <!-- navbar -->
            </div>
        </div> <!-- row -->
    </div> <!-- container -->
</div>
        <div id="home" class="header-content-area d-flex align-items-center list" style="height:450px;">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="header-wrapper">
                            <div class="header-content">
                                <h1 class="header-title">Ribbon（一）远程调用RestTemplate</h1>
                                <p class="text nav">
                                    <a href="/">首页</a> / <a href="/news/">新闻资讯</a> / <span>正文</span>
                                </p>
                            </div> <!-- header content -->
                            <div class="header-image d-none d-lg-block">
                                <div class="image">
                                    <img src="/assets/website/img/surfboardnode/header.png" alt="Header">
                                </div>
                            </div>
                        </div>
                    </div>
                </div> <!-- row -->
            </div> <!-- container -->
            <div class="header-shape">
                <img src="/assets/website/img/surfboardnode/header-shape.svg" alt="shape">
            </div> <!-- header-shape -->
        </div> <!-- header content area -->
    </header>
    <!--====== HEADER ONE PART ENDS ======-->
    <!--====== ABOUT THREE PART START ======-->
    <section id="about" class="about-area pt-70 pb-100">
        <div class="container">
            <div class="row">
                <div class="col-md-9">
                                    <input type="hidden" id="share-website-info" data-name="Clash Node官网订阅站" data-url="https://clashnode.github.io">
                  				  				  				<div id="content_views" class="htmledit_views"> <p id="main-toc"><strong>目录</strong></p> <p id="%E4%B8%80%E3%80%81RestTemplate%E4%BB%8B%E7%BB%8D-toc" style="margin-left:0px;"><a href="#%E4%B8%80%E3%80%81RestTemplate%E4%BB%8B%E7%BB%8D" rel="nofollow">一、RestTemplate介绍</a></p> <p id="%E4%BA%8C%E3%80%81%E9%9C%80%E6%B1%82%E8%AF%B4%E6%98%8E-toc" style="margin-left:0px;"><a href="#%E4%BA%8C%E3%80%81%E9%9C%80%E6%B1%82%E8%AF%B4%E6%98%8E" rel="nofollow">二、需求说明</a></p> <p id="1%E3%80%81%E4%B8%9A%E5%8A%A1%E7%BB%93%E6%9E%84%E5%8F%8A%E8%AF%B4%E6%98%8E-toc" style="margin-left:40px;"><a href="#1%E3%80%81%E4%B8%9A%E5%8A%A1%E7%BB%93%E6%9E%84%E5%8F%8A%E8%AF%B4%E6%98%8E" rel="nofollow">1、业务结构及说明</a></p> <p id="%E4%B8%89%E3%80%81%E6%A8%A1%E5%9D%97%E6%90%AD%E5%BB%BA-toc" style="margin-left:0px;"><a href="#%E4%B8%89%E3%80%81%E6%A8%A1%E5%9D%97%E6%90%AD%E5%BB%BA" rel="nofollow">三、模块搭建</a></p> <p id="1%E3%80%81%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AAmaven%E9%A1%B9%E7%9B%AE-toc" style="margin-left:40px;"><a href="#1%E3%80%81%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AAmaven%E9%A1%B9%E7%9B%AE" rel="nofollow">1、新建一个maven项目</a></p> <p id="2%E3%80%81%E6%B7%BB%E5%8A%A0ribbon%E4%BE%9D%E8%B5%96-toc" style="margin-left:40px;"><a href="#2%E3%80%81%E6%B7%BB%E5%8A%A0ribbon%E4%BE%9D%E8%B5%96" rel="nofollow">2、添加ribbon依赖</a></p> <p id="3%E3%80%81%E7%BC%96%E8%BE%91yml-toc" style="margin-left:40px;"><a href="#3%E3%80%81%E7%BC%96%E8%BE%91yml" rel="nofollow">3、编辑yml</a></p> <p id="4%E3%80%81%E4%B8%BB%E7%A8%8B%E5%BA%8F-toc" style="margin-left:40px;"><a href="#4%E3%80%81%E4%B8%BB%E7%A8%8B%E5%BA%8F" rel="nofollow">4、主程序</a></p> <p id="5%E3%80%81%E7%BC%96%E8%BE%91RibbonController-toc" style="margin-left:40px;"><a href="#5%E3%80%81%E7%BC%96%E8%BE%91RibbonController" rel="nofollow">5、编辑RibbonController</a></p> <p id="6%E3%80%81%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82%E6%B5%8B%E8%AF%95-toc" style="margin-left:40px;"><a href="#6%E3%80%81%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82%E6%B5%8B%E8%AF%95" rel="nofollow">6、发送请求测试</a></p> <hr id="hr-toc"/> <p>ribbon底层封装了RestTemplate进行Rest api远程调用</p> </h1> <p>RestTemplate是springboot提供的远程调用工具</p> <p>常用的方法：</p> <ul> <li> <span style="color:#ed7976;"><span style="background-color:#fef2f0;">getForObject()</span></span>：</li> </ul> <p>该方法是getForEntity方法的进一步封装，通过HttpMessageConverterExtractor对HTTP的请求响应体body内容进行对象转换，实现请求直接返回包装好的对象内容。有三个重载。</p> <p>getForObject(String url,Class responseType,Object ...urlVariables);</p> <p>url参数为指定访问的地址如果其中有传递的参数，使用{1}，{2}这种类型的占位符表示，responseType是参数返回结果的类型，urlVariables是前面url占位符对应的参数。</p> <p>getForObject(String url,Class responseType,Map urlVariables);</p> <p>前两个参数与前一致，最后一个参数使用Map封装替换url中出现的占位符。</p> <p>getForObject(String url,Class responseType);</p> <p>没有需要传递的参数。</p> <ul> <li><span style="color:#ed7976;"><span style="background-color:#fef2f0;">postForObject()</span></span></li> </ul> <p>用于发送post请求，与get类似</p> <p>也提供了三种重载：</p> <p>postForObject(String url,Class responseType,Object ...urlVariables);</p> <p>postForObject(String url,Class responseType,Map urlVariables);</p> <p>postForObject(String url,Class responseType);</p> </h1> <p>这里先不使用Ribbon，先单独使用RestTemplate进行远程调用</p> <h2 id="1%E3%80%81%E4%B8%9A%E5%8A%A1%E7%BB%93%E6%9E%84%E5%8F%8A%E8%AF%B4%E6%98%8E">1、业务结构及说明</h2> <p>当前是一个商品信息，用户和订单三者之间的服务，并且使用Eureka集群提供服务发现与注册</p> <p>结构及功能为：</p> <p><img fetchpriority="high" decoding="async" alt="" height="1200" src="http://img.555519.xyz/uploads/20230205/b6ab68088720eff85551991d951a730c.jpg"></p> <p>其中</p> <p>item-service 用于商品服务，其中有获取商品信息和减少库存的方法</p> <p>user-service 用于用户服务，其中有获取用户信息和增加用户积分的功能</p> <p>order-service 用于订单服务，其中有获取订单和添加订单的功能</p> <p>现在需要使用RestTemplate进行远程调用这三个服务</p> </h1> <h2 id="1%E3%80%81%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AAmaven%E9%A1%B9%E7%9B%AE">1、新建一个maven项目</h2> <h2 id="2%E3%80%81%E6%B7%BB%E5%8A%A0ribbon%E4%BE%9D%E8%B5%96">2、添加ribbon依赖</h2> <p>由于Eureka-client中包含ribbon依赖，所以只需添加eureka-client依赖即可</p> <p><img decoding="async" alt="" height="317" src="http://img.555519.xyz/uploads/20230205/e90e107ff74cf4e334092716d99f4828.jpg"></p> <pre><code class="language-XML">&lt;dependency&gt; 	&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt; 	&lt;artifactId&gt;spring-cloud-starter-netflix-eureka-client&lt;/artifactId&gt; &lt;/dependency&gt; </code></pre> <h2 id="3%E3%80%81%E7%BC%96%E8%BE%91yml">3、编辑yml</h2> <p>服务名称为ribbon，使用3001端口，并且注册到两台eureka服务器中</p> <pre><code>spring:   application:     name: ribbon      server:   port: 3001    eureka:   client:     service-url:       defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka </code></pre> <h2 id="4%E3%80%81%E4%B8%BB%E7%A8%8B%E5%BA%8F">4、主程序</h2> <p>添加RestTemplate实例</p> <pre><code class="language-java">package cn.tedu.sp06;  ....  @EnableDiscoveryClient @SpringBootApplication public class Sp06RibbonApplication { 	 	//创建 RestTemplate 实例，并存入 spring 容器 	@Bean 	public RestTemplate getRestTemplate() { 		return new RestTemplate(); 	}  	public static void main(String[] args) { 		SpringApplication.run(Sp06RibbonApplication.class, args); 	}  } </code></pre> <h2 id="5%E3%80%81%E7%BC%96%E8%BE%91RibbonController">5、编辑RibbonController</h2> <pre><code class="language-java">package cn.tedu.sp06.controller;  ...  @RestController public class RibbonController {  	@Autowired 	private RestTemplate rt; 	 	@GetMapping("/item-service/{orderId}") 	public JsonResult&lt;List&lt;Item&gt;&gt; getItems(@PathVariable String orderId) { 	    //向指定微服务地址发送 get 请求，并获得该服务的返回结果  	    //{1} 占位符，用 orderId 填充 		return rt.getForObject("http://localhost:8001/{1}", JsonResult.class, orderId); 	}  	@PostMapping("/item-service/decreaseNumber") 	public JsonResult decreaseNumber(@RequestBody List&lt;Item&gt; items) { 	    //发送 post 请求 		return rt.postForObject("http://localhost:8001/decreaseNumber", items, JsonResult.class); 	}  	// 	 	@GetMapping("/user-service/{userId}") 	public JsonResult&lt;User&gt; getUser(@PathVariable Integer userId) { 		return rt.getForObject("http://localhost:8101/{1}", JsonResult.class, userId); 	}  	@GetMapping("/user-service/{userId}/score")  	public JsonResult addScore( 			@PathVariable Integer userId, Integer score) { 		return rt.getForObject("http://localhost:8101/{1}/score?score={2}", JsonResult.class, userId, score); 	} 	 	// 	 	@GetMapping("/order-service/{orderId}") 	public JsonResult&lt;Order&gt; getOrder(@PathVariable String orderId) { 		return rt.getForObject("http://localhost:8201/{1}", JsonResult.class, orderId); 	}  	@GetMapping("/order-service") 	public JsonResult addOrder() { 		return rt.getForObject("http://localhost:8201/", JsonResult.class); 	} } </code></pre> <p><strong>注意：</strong></p> <p><strong>1、可以看出，用户发送请求首先到RibbonController然后通过这里转发到对应的服务路径，实现远程调用。</strong></p> <p><strong>2、携带参数的GET请求，会使用{1}表示占位符进行转发。</strong></p> <h2 id="6%E3%80%81%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82%E6%B5%8B%E8%AF%95">6、发送请求测试</h2> <p>根据RibbonController的路径发送请求</p> <p><span style="color:#ed7976;"><span style="background-color:#fef2f0;">http://localhost:3001/item-service/35</span></span></p> <p>使用postman发送</p> <p><span style="color:#ed7976;"><span style="background-color:#fef2f0;">http://localhost:3001/item-service/decreaseNumber</span></span></p> <p><span style="color:#ed7976;"><span style="background-color:#fef2f0;">[{"id":1,"name":"abc","number":100},{"id":2,"name":"def","number":200}]</span></span></p> <p><span style="color:#ed7976;"><span style="background-color:#fef2f0;">http://localhost:3001/user-service/10</span></span></p> <p><span style="color:#ed7976;"><span style="background-color:#fef2f0;">http://localhost:3001/user-service/10/score?score=100</span></span></p> <p><span style="color:#ed7976;"><span style="background-color:#fef2f0;">http://localhost:3001/order-service/20</span></span></p> <p><span style="color:#ed7976;"><span style="background-color:#fef2f0;">http://localhost:3001/order-service</span></span></p> <p>浏览器发送请求先到ribbon服务器的RibbonController，之后通过RestTemplate的getForObject或postForObject方法分别远程调用到item-service、user-service、ordder-service。</p> <p><img decoding="async" alt="" height="1153" src="http://img.555519.xyz/uploads/20230205/94f1d00cbaeb44dc9b98041c9167c903.jpg"></p> </p></div> 			                <div class="clearfix"></div>
                <div class="col-md-12 mt-5">
                                        <p>上一个：<a href="/news/article-97278.htm">宠物领养小程序的市场前景（宠物领养 app）</a></p>
                                        <p>下一个：<a href="/news/article-98092.htm">动物疫苗产业链（动物疫苗行业分析）</a></p>
                                    </div>
                                </div>
                <div class="col-md-3">
                    <div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">热门文章</h3>
    </div>
    <div class="panel-body">
        <ul class="p-0 x-0" style="list-style: none;margin: 0;padding: 0;">
                        <li class="py-2"><a href="/news/article-95877.htm" title="C语言 模拟实现strlen函数详解_C 语言">C语言 模拟实现strlen函数详解_C 语言</a></li>
                        <li class="py-2"><a href="/free-nodes/2025-3-29-free-high-speed-nodes.htm" title="V2ray免费机场 | 3月29日21.9M/S|免费SSR/Shadowrocket/Clash/V2ray订阅节点地址分享">V2ray免费机场 | 3月29日21.9M/S|免费SSR/Shadowrocket/Clash/V2ray订阅节点地址分享</a></li>
                        <li class="py-2"><a href="/news/article-95143.htm" title="动物医院临床技术以下为胸腹腔渗出液的性质的是（能进行动物胸腔手术的动物诊疗机构）">动物医院临床技术以下为胸腹腔渗出液的性质的是（能进行动物胸腔手术的动物诊疗机构）</a></li>
                        <li class="py-2"><a href="/news/article-91109.htm" title="宠物粮食价格表图片高清（宠物粮食图片大全）">宠物粮食价格表图片高清（宠物粮食图片大全）</a></li>
                        <li class="py-2"><a href="/free-nodes/2025-4-16-clash-v2ray-ss-ssr.htm" title="V2ray免费VPN机场 | 4月16日22.6M/S|免费Shadowrocket/Clash/V2ray/SSR订阅节点地址分享">V2ray免费VPN机场 | 4月16日22.6M/S|免费Shadowrocket/Clash/V2ray/SSR订阅节点地址分享</a></li>
                        <li class="py-2"><a href="/news/article-92153.htm" title="北京动物防疫站地址（北京动物疫病防控中心）">北京动物防疫站地址（北京动物疫病防控中心）</a></li>
                        <li class="py-2"><a href="/news/article-94453.htm" title="宠物医院大众点评评论文案大全（宠物医院大众点评评语）">宠物医院大众点评评论文案大全（宠物医院大众点评评语）</a></li>
                        <li class="py-2"><a href="/news/article-88400.htm" title="动物疫苗检测机构电话（动物疫苗检测机构电话查询）">动物疫苗检测机构电话（动物疫苗检测机构电话查询）</a></li>
                        <li class="py-2"><a href="/news/article-92840.htm" title="济南 宠物领养 济南宠物领养">济南 宠物领养 济南宠物领养</a></li>
                        <li class="py-2"><a href="/news/article-84855.htm" title="2021年公务员国考报名时间（2021公务员国考报考时间）">2021年公务员国考报名时间（2021公务员国考报考时间）</a></li>
                    </ul>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">归纳</h3>
    </div>
    <div class="panel-body">
        <ul class="p-0 x-0" style="list-style: none;margin: 0;padding: 0;">
                        <li class="py-2">
                <h4><span class="badge" style="float: right;">57</span> <a href="/date/2025-04/" title="2025-04 归档">2025-04</a></h4>
            </li>
                        <li class="py-2">
                <h4><span class="badge" style="float: right;">15</span> <a href="/date/2025-03/" title="2025-03 归档">2025-03</a></h4>
            </li>
                    </ul>
    </div>
</div>

                </div>
            </div>
        </div> <!-- container -->
    </section>
    <!--====== ABOUT THREE PART ENDS ======-->
        <!--====== FOOTER PART START ======-->
    <footer id="footer" class="footer-area">
        <div class="footer-copyright">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="copyright text-center">
                                                <p>
                                                <a href="/">首页</a> |
                                                <a href="/free-nodes/">免费节点</a> |
                                                <a href="/paid-subscribe/">推荐机场</a> |
                                                <a href="/news/">新闻资讯</a> |
                                                <a href="/client.htm">客户端</a> |
                                                <a href="/about-us.htm">关于我们</a> |
                        <a href="/disclaimer.htm">免责申明</a> |
                        <a href="/privacy.htm">隐私申明</a> |
                        <a href="/sitemap.xml">网站地图</a>
                    </p>
                            <p class="text">V2ray免费机场 节点订阅中文官网 版权所有 Powered by WordPress</p>
                        </div> <!-- copyright -->
                    </div>
                </div> <!-- row -->
            </div> <!-- container -->
        </div> <!-- footer copyright -->
    </footer>
    <!--====== FOOTER PART ENDS ======-->
    <!--====== BACK TOP TOP PART START ======-->
    <a href="#" class="back-to-top"><i class="lni-chevron-up"></i></a>
    <!--====== BACK TOP TOP PART ENDS ======-->
    <!--====== jquery js ======-->
    <script src="/assets/website/js/frontend/surfboardnode/vendor/modernizr-3.6.0.min.js"></script>
    <script src="/assets/website/js/frontend/surfboardnode/vendor/jquery-1.12.4.min.js"></script>
    <!--====== Bootstrap js ======-->
    <script src="/assets/website/js/frontend/surfboardnode/bootstrap.min.js"></script>
    <script src="/assets/website/js/frontend/surfboardnode/popper.min.js"></script>
    <!--====== Images Loaded js ======-->
    <script src="/assets/website/js/frontend/surfboardnode/imagesloaded.pkgd.min.js"></script>
    <!--====== Scrolling Nav js ======-->
    <script src="/assets/website/js/frontend/surfboardnode/jquery.easing.min.js"></script>
    <script src="/assets/website/js/frontend/surfboardnode/scrolling-nav.js"></script>
    <!--====== Slick js ======-->
    <script src="/assets/website/js/frontend/surfboardnode/slick.min.js"></script>
    <!--====== Main js ======-->
    <script src="/assets/website/js/frontend/surfboardnode/main.js"></script>
    <script src="https://www.freeclashnode.com/assets/js/frontend/invite-url.js"></script>
    <script src="/assets/website/js/frontend/G.js"></script>
</body>

</html>